package com.freight.tws.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.constant.Constants;
import com.freight.common.core.domain.R;
import com.freight.common.core.web.page.Condition;
import com.freight.common.core.web.page.Query;
import com.freight.common.security.utils.SecurityUtils;
import com.freight.tws.api.domain.Vehicle;
import com.freight.tws.service.IVehicleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 手机端 - 车辆controller
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping(Constants.APP_URL + "/")
public class AppVehicleController {

    private final IVehicleService iVehicleService;

    /**
     * 车辆列表，分页
     * @param
     * @return
     */
    @GetMapping("car-list")
    public R<IPage<Vehicle>> carList(Query query, @RequestParam String type) {
        LambdaQueryWrapper<Vehicle> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vehicle::getType, type);
        lqw.eq(Vehicle::getDriverId, SecurityUtils.getUserId());
        lqw.orderByDesc(Vehicle::getCreateTime);
        IPage<Vehicle> list = iVehicleService.page(Condition.getPage(query), lqw);
        return R.ok(list);
    }

    /** 车辆列表，不分页 */
    @GetMapping("/cars")
    public R cars(@RequestParam String type) {
        List<Vehicle> list = iVehicleService.list(new LambdaQueryWrapper<Vehicle>()
                .eq(Vehicle::getType, type)
                .eq(Vehicle::getDriverId, SecurityUtils.getUserId())
                .orderByDesc(Vehicle::getCreateTime));
        return R.ok(list);
    }

    /** 新增车辆 */
    @PostMapping("car-add")
    public R add(@RequestBody Vehicle vehicle) {
        return iVehicleService.add(vehicle) ? R.ok() : R.fail();
    }

    /** 详情 */
    @GetMapping(value = "car-info/{id}")
    public R getInfo(@PathVariable("id") Long id) {
        return R.ok(iVehicleService.getById(id));
    }

    /** 编辑 */
    @PostMapping("car-edit")
    public R edit(@RequestBody Vehicle car) {
        Vehicle byId = iVehicleService.getById(car.getId());
        if(byId == null) {
            return R.fail("车辆不存在");
        }
        // 如果当前车牌号和数据库中存的不一致，说明修改了，去查修改后的车牌号是否重复
//        if(!byId.getVehicleNumber().equals(car.getVehicleNumber())) {
//            int count = iVehicleService.count(new LambdaQueryWrapper<Vehicle>()
//                                .eq(Vehicle::getVehicleNumber, car.getVehicleNumber()));
//            if(count > 0) {
//                return R.fail("车牌号重复");
//            }
//        }
        return iVehicleService.updateById(car) ? R.ok() : R.fail();
    }

    /**
     * 删除货车信息
     */
    @GetMapping("car-remove/{id}")
    public R remove(@PathVariable Long id) {
        return iVehicleService.removeById(id) ? R.ok() : R.fail();
    }

}
